﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml.Serialization;
using LogFileScanner.Properties;

namespace LogFileScanner
{

    public class ScanSetting
    {
        private string name = "Default";
        private string[] paths = null;

        public ScanSetting()
        {
        }

        public ScanSetting(string name)
        {
            this.Name = name;
        }

        public string Name
        {
            get { return name; }
            set
            {
                bool valid = value.IndexOfAny(Path.GetInvalidFileNameChars()) < 0;
                if (valid)
                {
                    this.name = value;
                }
                else
                {
                    throw new FieldAccessException("Invalid name for the scan settings file: " + value + ", the name must be valid file name.");
                }
            }
        }

        public string FilePath
        {
            get
            {
                if (string.IsNullOrEmpty(Settings.Default.SettingsPath))
                {
                    Settings.Default.SettingsPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }

                string settingsPath = Path.GetFullPath(Settings.Default.SettingsPath);

                return Path.Combine(new string[] { settingsPath, Name + ".scan" });
            }
        }

        public string FileFilter
        {
            get;
            set;
        }

        public DateTime FilterFromDate
        {
            get;
            set;
        }

        public DateTime FilterToDate
        {
            get;
            set;
        }

        public bool Loaded { get; set; }

        public string[] Paths
        {
            get
            {
                if (paths == null)
                {
                    paths = new string[] { };
                }

                return paths;
            }
            set
            {
                this.paths = value;
            }
        }

        public void Load()
        {
            XmlSerializer reader = new XmlSerializer(typeof(ScanSetting));

            using (StreamReader fileReader = new StreamReader(FilePath))
            {
                object result = reader.Deserialize(fileReader);
                if (result is ScanSetting)
                {
                    ScanSetting loadedSetting = result as ScanSetting;
                    this.Name = loadedSetting.name;
                    this.Paths = loadedSetting.paths;
                    this.FilterFromDate = loadedSetting.FilterFromDate;
                    this.FilterToDate = loadedSetting.FilterToDate;
                    this.FileFilter = loadedSetting.FileFilter;
                }

                Loaded = true;
            }
        }

        public void Store()
        {
            XmlSerializer writer = new XmlSerializer(typeof(ScanSetting));

            using (StreamWriter fileWriter = new StreamWriter(FilePath))
            {
                writer.Serialize(fileWriter, this);

                Loaded = true;
            }

        }

        public void Remove()
        {
            if (File.Exists(FilePath))
            {
                File.Delete(FilePath);

                Loaded = false;
            }
        }

        public override string ToString()
        {
            return Name;
        }
    }
}
